define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'taskvideoheat/index',
                    // add_url: 'vipuser/add',
                    // edit_url: 'anchor/edit',
                    // del_url: 'anchor/del',
                    // multi_url: 'anchor/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'livestreamerUser',
                sortName: 'taskBeginTime',
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'livestreamerUser', title: __('Host ID'), operate: 'like'},
                        {field: 'zone_list.zonename', title: __('District'), searchList: Config.zoneList},
                        {field: 'videoURL', title: __('Short Video Links'), operate: false},
                        {
                            field: 'taskBeginTime',
                            title: __('taskBeginTime'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: false
                        },
                        {
                            field: 'taskBeginTime',
                            title: __('taskBeginTime'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: 'RANGE',
                            visible: false
                        },
                        {
                            field: 'taskEndTime',
                            title: __('taskEndTime'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: false
                        },
                        {field: 'taskNum', title: __('No. of Views Request'), operate: false},
                        {field: 'completeNum', title: __('No. of Completion'), operate: false}
                    ]
                ]
            });

            $(document).on("click", ".btn-addjob", function () {
                var ids = Table.api.selectedids(table);
                var url = 'taskvideoheat/addjob';
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        addjob: function () {
            $(".btn-searchUser").on("click", function () {
                let id = $("#c-livestreamerUser").val();
                if (id == '' || id == null) {
                    Backend.api.toastr.error('请输入主播id');
                    return
                }
                // $.get("taskvideoheat/searchUser", {id : id}, function (data) {
                //     if (data.code == 500) {
                //         Backend.api.toastr.error(data.msg);
                //     }
                //
                //     $("#userNickName").text("短视频加热权益：" + data.videoheatSurplusNum);
                //
                //     data.data.forEach((item, index) => {
                //         var unixTimestamp = new Date(item.create_time * 1000);
                //
                //         $("#img" + index).attr("src", item.url_list)
                //         $("#uri" + index).val(item.uri)
                //         $("#video" + index).text("发布时间：" + unixTimestamp.toLocaleString())
                //     })
                // })
                Fast.api.ajax({
                    url: 'taskvideoheat/searchUser?id=' + id,
                    loading: true
                }, function (data) {
                    $("#userNickName").text("短视频加热权益：" + data.videoheatSurplusNum);
                    //
                    // data.data.forEach((item, index) => {
                    //     var unixTimestamp = new Date(item.create_time * 1000);
                    //
                    //     $("#img" + index).attr("src", item.url_list)
                    //     $("#uri" + index).val(item.uri)
                    //     $("#video" + index).text("发布时间：" + unixTimestamp.toLocaleString())
                    // })

                }, function (data, ret) {
                    Layer.msg(ret.msg)
                    return false
                })
            })

            $("#img0").on("click", function () {
                $("#c-videoURL").val($("#uri0").val())
                $(this).css("border", "5px solid #ccc")
                $("#img1").css("border", "none")
            })

            $("#img1").on("click", function () {
                $("#c-videoURL").val($("#uri1").val())
                $(this).css("border", "5px solid #ccc")
                $("#img0").css("border", "none")
            })

            Controller.api.bindevent()
        }
    };
    return Controller;
});
